package com.bos.controller.json;

import com.bos.dao.InstansiDAO;
import com.bos.dao.JabatanDAO;
import com.bos.dao.PengelolaDAO;
import com.bos.domain.Instansi;
import com.bos.domain.Jabatan;
import com.bos.domain.Pengelola;
import com.bos.editor.InstansiEditor;
import com.bos.editor.JabatanEditor;
import com.bos.editor.PengelolaEditor;
import com.bos.model.PengelolaRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * User: Latief
 * Date: 3/17/11
 * Time: 8:29 PM
 * To change this template use File | Settings | File Templates.
 */
@Controller
public class PengelolaJsonController {

    private PengelolaDAO pengelolaDAO;
    private InstansiDAO instansiDAO;
    private JabatanDAO jabatanDAO;

/* --------------------- Getter and Setter ---------------------*/

    @Autowired
    public void setPengelolaDAO(PengelolaDAO pengelolaDAO) {
        this.pengelolaDAO = pengelolaDAO;
    }

    @Autowired
    public void setInstansiDAO(InstansiDAO instansiDAO) {
        this.instansiDAO = instansiDAO;
    }

    @Autowired
    public void setJabatanDAO(JabatanDAO jabatanDAO) {
        this.jabatanDAO = jabatanDAO;
    }

    /**
     * ************** JSON **************************************************************************
     */

    @RequestMapping(value = "/pengelola.json", method = RequestMethod.POST)
    public
    @ResponseBody
    Map<String, Object> save(@RequestBody PengelolaRequest pengelolaRequest) {
        if (pengelolaRequest == null) throw new IllegalArgumentException("A Product is required");
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            Pengelola pengelola = pengelolaRequest.convertTo();
            pengelola.setId(0);
            pengelolaDAO.save(pengelola);
            result.put("success", true);
            result.put("pengelolaRequest", pengelolaRequest);
            result.put("message", "Sukses Save Pengelola " + pengelolaRequest);
        } catch (DataAccessException e) {
            result.put("success", false);
            result.put("pengelolaRequest", pengelolaRequest);
            result.put("exception", e.getMessage());
            result.put("message", "Gagal Save Pengelola " + pengelolaRequest);
        }
        return result;
    }

    @RequestMapping(value = "/pengelola/{id}/delete.json", method = RequestMethod.GET)
    public
    @ResponseBody
    Map<String, Object> delete(@PathVariable("id") PengelolaRequest pengelolaRequest) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            pengelolaDAO.delete(pengelolaRequest.convertTo());
            result.put("success", true);
            result.put("message", "Sukses Delete Pengelola " + pengelolaRequest);
        } catch (DataAccessException e) {
            result.put("success", false);
            result.put("exception", e.getMessage());
            result.put("message", "Gagal Delete Pengelola " + pengelolaRequest);
        }
        return result;
    }

    @RequestMapping(value = "/pengelola.json", method = RequestMethod.PUT)
    public
    @ResponseBody
    Map<String, Object> update(@RequestBody PengelolaRequest pengelolaRequest) {
        if (pengelolaRequest == null) throw new IllegalArgumentException("A Product is required");
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            pengelolaDAO.merge(pengelolaRequest.convertTo());
            result.put("success", true);
            result.put("pengelolaRequest", pengelolaRequest);
            result.put("message", "Sukses Update Pengelola " + pengelolaRequest);
        } catch (DataAccessException e) {
            result.put("success", false);
            result.put("pengelolaRequest", pengelolaRequest);
            result.put("message", "Gagal Update Pengelola " + pengelolaRequest);
        }
        return result;
    }

    @InitBinder
    protected void initBinder(WebDataBinder binder) throws Exception {
        binder.registerCustomEditor(Instansi.class, new InstansiEditor(instansiDAO));
        binder.registerCustomEditor(Jabatan.class, new JabatanEditor(jabatanDAO));
        binder.registerCustomEditor(Pengelola.class, new PengelolaEditor(pengelolaDAO));
    }

}
